import 'package:flutter/material.dart';



class LoginForm extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _LoginForm();
  }
}

class _LoginForm extends State<LoginForm> {
  final registerFormKey = GlobalKey<FormState>();


  String username,password;
  bool autoValidate = false;

  String validateUsername(value) {
    if (value.isEmpty) {
      return 'Username is required.';
    }
    return null;
  }

  String validatePassword(value) {
    if (value.isEmpty) {
      return 'Password is required.';
    }
    return null;
  }

  void submitRegisterForm() {
    if (registerFormKey.currentState.validate()) {
      registerFormKey.currentState.save();

      debugPrint('username: $username');
      debugPrint('password: $password');
      FocusScope.of(context).requestFocus(FocusNode());
      Scaffold.of(context).showSnackBar(
          SnackBar(
            content: Text('Registering...')
          )
      );

    } else {
      setState(() {
        autoValidate = true;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Form(
      key: registerFormKey,
      child: Column(
        children: <Widget>[
          TextFormField(
            decoration: InputDecoration(
                labelText: '用户名/手机号',
                helperText: '',
                prefixIcon: Icon(Icons.person,color: Colors.pink,)
            ),
            onSaved: (value) {
              username = value;
            },
            validator: validateUsername,
            autovalidate: autoValidate,
          ),
          TextFormField(
            obscureText: true,
            decoration: InputDecoration(
              labelText: '密码',
              helperText: '',
              prefixIcon: Icon(Icons.lock,color: Colors.pink,),
            ),
            onSaved: (value) {
              password = value;
            },
            validator: validatePassword,
            autovalidate: autoValidate,
          ),
          SizedBox(height: 32.0,),
          Container(
            width: double.infinity,
            child: RaisedButton(
              color: Theme.of(context).accentColor,
              child: Text('登 录', style: TextStyle(color: Colors.white,fontSize: 16.0,wordSpacing: 10.0)),
              elevation: 0.0,
              onPressed: submitRegisterForm,
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text('忘记密码'),
              Text('快速注册')
            ],
          )
        ],
      ),
    );
  }
}
